home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / longfile / longfile.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-08-12  |  19.2 KB  |  524 lines

  1. VERSION 2.00
  2. Begin Form frmLongFile 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Open File "
  6.    ClientHeight    =   3270
  7.    ClientLeft      =   1110
  8.    ClientTop       =   1500
  9.    ClientWidth     =   7005
  10.    ControlBox      =   0   'False
  11.    Height          =   3675
  12.    Left            =   1050
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   3270
  17.    ScaleWidth      =   7005
  18.    Top             =   1155
  19.    Width           =   7125
  20.    Begin TextBox txtFilename 
  21.       Height          =   315
  22.       Left            =   120
  23.       TabIndex        =   11
  24.       Top             =   360
  25.       Width           =   2895
  26.    End
  27.    Begin ComboBox comboFileTypes 
  28.       Height          =   315
  29.       Left            =   120
  30.       TabIndex        =   6
  31.       Text            =   "Combo1"
  32.       Top             =   2820
  33.       Width           =   2895
  34.    End
  35.    Begin DirListBox Dir1 
  36.       Height          =   1605
  37.       Left            =   3120
  38.       TabIndex        =   5
  39.       Top             =   840
  40.       Width           =   2535
  41.    End
  42.    Begin DriveListBox Drive1 
  43.       Height          =   315
  44.       Left            =   3120
  45.       TabIndex        =   4
  46.       Top             =   2820
  47.       Width           =   2535
  48.    End
  49.    Begin CommandButton btnCancel 
  50.       Caption         =   "Cancel"
  51.       Height          =   375
  52.       Left            =   5760
  53.       TabIndex        =   2
  54.       Top             =   1320
  55.       Width           =   1095
  56.    End
  57.    Begin CommandButton btnOpen 
  58.       Caption         =   "OK"
  59.       Height          =   375
  60.       Left            =   5760
  61.       TabIndex        =   1
  62.       Top             =   840
  63.       Width           =   1095
  64.    End
  65.    Begin ListBox List1 
  66.       Height          =   1590
  67.       Left            =   120
  68.       Sorted          =   -1  'True
  69.       TabIndex        =   0
  70.       Top             =   840
  71.       Width           =   2895
  72.    End
  73.    Begin Label Label5 
  74.       AutoSize        =   -1  'True
  75.       BackColor       =   &H00C0C0C0&
  76.       Caption         =   "Folders:"
  77.       Height          =   195
  78.       Left            =   3120
  79.       TabIndex        =   10
  80.       Top             =   120
  81.       Width           =   690
  82.    End
  83.    Begin Label Label4 
  84.       AutoSize        =   -1  'True
  85.       BackColor       =   &H00C0C0C0&
  86.       Caption         =   "Filename:"
  87.       Height          =   195
  88.       Left            =   120
  89.       TabIndex        =   9
  90.       Top             =   120
  91.       Width           =   825
  92.    End
  93.    Begin Label Label3 
  94.       AutoSize        =   -1  'True
  95.       BackColor       =   &H00C0C0C0&
  96.       Caption         =   "Drives:"
  97.       Height          =   195
  98.       Left            =   3120
  99.       TabIndex        =   8
  100.       Top             =   2580
  101.       Width           =   615
  102.    End
  103.    Begin Label Label2 
  104.       AutoSize        =   -1  'True
  105.       BackColor       =   &H00C0C0C0&
  106.       Caption         =   "List files of type:"
  107.       Height          =   195
  108.       Left            =   120
  109.       TabIndex        =   7
  110.       Top             =   2580
  111.       Width           =   1425
  112.    End
  113.    Begin Label lblFolders 
  114.       BackColor       =   &H00C0C0C0&
  115.       BorderStyle     =   1  'Fixed Single
  116.       Height          =   375
  117.       Left            =   3120
  118.       TabIndex        =   3
  119.       Top             =   360
  120.       Width           =   3735
  121.       WordWrap        =   -1  'True
  122.    End
  123. Option Explicit
  124. ' This form and its accompanying longfile.bas module allow 16-bit VB
  125. ' applications to use long file names when run in environments (Windows 95
  126. ' and Windows NT) that support them.  Set up for a call to this form is
  127. ' much like a call to a common dialog box, but with considerably less
  128. ' properties.  The properties are stored in the following structure:
  129. '' structure for dialog box setup
  130. 'Type LongFile
  131. '   Action as Integer         ' 1 = Open, 2 = Save
  132. '   Color As Long             ' background color
  133. '   DialogTitle As String     ' title bar text
  134. '   Filename As String        ' filename for input to dialog box, output filename will be in gShortFilename and gLongFilename
  135. '   Filter As String          ' file extension filter
  136. '   FilterIndex As Integer    ' index into file extension filter
  137. 'End Type
  138. ' This structure is declared as LF and the declaration is global.  Note
  139. ' one major difference between this structure and that of the common dialog
  140. ' box:  the Filename string is used only to send a name to this form.  The
  141. ' string will be null upon exit from this form.  The user selected filename
  142. ' will be in two global variables, gShortFilename and gLongFilename.  Both
  143. ' will contain the full path to the filename.  Another global variable,
  144. ' gIn16BitSystem will be set to True if the system only supports short
  145. ' filenames, False if the system supports long filenames.  Use this value
  146. ' to determine which of gShortFilename or gLongFilename to use to interact
  147. ' with the system for file saves and opens.  In 16-bit Windows systems, both
  148. ' gShortFilename and gLongFilename will contain the same value.
  149. ' Sample setup and call:
  150. '     LF.Action = 1     ' nothing happens until call to GetLongFilename
  151. '     LF.DialogTitle = "Select File to Open"
  152. '     LF.Filter = "Text (*.txt)|*.txt|HTML (*.htm)|*.htm|All Files (*.*)|*.*"
  153. '     LF.FilterIndex = 2   ' set html as default file type
  154. '     GetLongFilename
  155. ' Another example:
  156. '     LF.Action = 2     ' nothing happens until call to GetLongFilename
  157. '     LF.DialogTitle = "Save File"
  158. '     LF.Filename = "foo.txt"
  159. '     GetLongFilename
  160. '     if LF.Action = -1 ' then user chose Cancel
  161. ' Since the LF structure is global, structure values remain intact between
  162. ' calls except for LF.Filename, which is cleared after each call to
  163. ' GetLongFilename, and LF.Action, which is set to zero on normal exit or
  164. ' -1 if the user selected the Cancel button.
  165. '============================================================================
  166. ' form level declarations for long filename support
  167. Dim hInstKernel As Long
  168. Dim lpGetShortPathNameA As Long
  169. Dim lpFindFirstFileA As Long
  170. ' api calls for long filename support
  171. Declare Function LoadLibraryEx32W Lib "KERNEL" (ByVal lpszFile As String, ByVal hFile As Long, ByVal dwFlags As Long) As Long
  172. Declare Function FreeLibrary32W Lib "KERNEL" (ByVal hDllModule As Long) As Long
  173. Declare Function GetProcAddress32W Lib "KERNEL" (ByVal hInstance As Long, ByVal FunctionName As String) As Long
  174. Declare Function FindFirstFileA Lib "KERNEL" Alias "CallProc32W" (ByVal lpszFile As String, aFindFirst As WIN32_FIND_DATA, ByVal lpfnFunction As Long, ByVal fAddressConvert As Long, ByVal dwParams As Long) As Long
  175. Declare Function GetShortPathNameA Lib "KERNEL" Alias "CallProc32W" (ByVal lpszLongFile As String, ByVal lpszShortFile As String, ByVal lBuffer As Long, ByVal lpfnFunction As Long, ByVal fAddressConvert As Long, ByVal dwParams As Long) As Long
  176. Declare Function lcreat Lib "Kernel" Alias "_lcreat" (ByVal lpPathName As String, ByVal iAttribute As Integer) As Integer
  177. Sub btnCancel_Click ()
  178.    gShortFilename = ""
  179.    gLongFilename = ""
  180.    LF.Action = -1
  181.    Unload Me
  182. End Sub
  183. ' copyright 1996, Internet Software Engineering
  184. Sub btnOpen_Click ()
  185.    Dim szShortFilename As String * 256
  186.    Dim p As Integer
  187.    Dim a As Long
  188.    Dim tmpstr As String
  189.    Dim hFile As Integer
  190.    Dim rtn As Integer
  191.    tmpstr = lblFolders.Caption
  192.    If Right$(tmpstr, 1) <> "\" Then tmpstr = tmpstr & "\"
  193.    gLongFilename = tmpstr & txtFilename.Text
  194.    'Convert the Long Filename to a Short Filename
  195.    If LF.Action = 2 And Not gIn16BitSystem Then ' create the file
  196.       hFile = lcreat(gLongFilename, 0)
  197.       If hFile = -1 Then
  198.      MsgBox "File error.", 16, App.Title
  199.       End If
  200.    End If
  201.    If Not gIn16BitSystem Then
  202.       a = GetShortPathNameA(gLongFilename, szShortFilename, 256&, lpGetShortPathNameA, 6&, 3&)
  203.       p = InStr(szShortFilename, Chr$(0))
  204.       gShortFilename = Left$(szShortFilename, p - 1)
  205.    Else
  206.       gShortFilename = gLongFilename
  207.    End If
  208.    Unload Me
  209. End Sub
  210. ' copyright 1996, Internet Software Engineering
  211. Function ChangeLongFilenameToShort (Filename As String) As Integer
  212.    ' the return value from this function seems backwards, but is correct
  213.    ' returning false means we're not in a 16-bit system
  214.    ' returning true means we are
  215.    On Error GoTo ChangeLongFilenameToShort_Error
  216.    Dim sFF As WIN32_FIND_DATA
  217.    Dim a As Long
  218.    Dim szShortFilename As String * 256
  219.    Dim p As Integer
  220.    ' load Kernel32
  221.    hInstKernel = LoadLibraryEx32W("Kernel32.dll", 0&, 0&)
  222.    ' get the address of the functions to deal with long filenames
  223.    lpGetShortPathNameA = GetProcAddress32W(hInstKernel, "GetShortPathNameA")
  224.    ' change the filename
  225.    ' get the short name for the directory currently selected and clean it up
  226.    a = GetShortPathNameA(Filename, szShortFilename, 256&, lpGetShortPathNameA, 6&, 3&)
  227.    p = InStr(szShortFilename, Chr$(0))
  228.    ChangeLongFilenameToShort = False
  229.    gLongFilename = Filename
  230.    gShortFilename = LCase$(Left$(szShortFilename, p - 1))
  231.    ' release the Kernel if necessary
  232.    a = FreeLibrary32W(hInstKernel)
  233.    Exit Function
  234. ChangeLongFilenameToShort_Error:
  235.    ' must be no Win32 support, so just return the passed in filename
  236.    ChangeLongFilenameToShort = True
  237.    gLongFilename = Filename
  238.    gShortFilename = Filename
  239.    Exit Function
  240. End Function
  241. ' copyright 1996, Internet Software Engineering
  242. Function ChangeShortFilenameToLong (Filename As String) As Integer
  243.    ' the return value from this function seems backwards, but is correct
  244.    ' returning false means we're not in a 16-bit system
  245.    ' returning true means we are
  246.    On Error GoTo ChangeShortFilenameToLong_Error
  247.    Dim sFF As WIN32_FIND_DATA
  248.    Dim a As Long
  249.    Dim szShortFilename As String * 256
  250.    ' load Kernel32
  251.    hInstKernel = LoadLibraryEx32W("Kernel32.dll", 0&, 0&)
  252.    ' get the address of the functions to deal with long filenames
  253.    lpFindFirstFileA = GetProcAddress32W(hInstKernel, "FindFirstFileA")
  254.    'Use the Win32 call to convert any short filenames to long filenames
  255.    a = FindFirstFileA(Filename, sFF, lpFindFirstFileA, 3&, 2&)
  256.    gLongFilename = sFF.cFileName
  257.    gShortFilename = Filename
  258.    ChangeShortFilenameToLong = False
  259.    ' release the Kernel if necessary
  260.    a = FreeLibrary32W(hInstKernel)
  261.    Exit Function
  262. ChangeShortFilenameToLong_Error:
  263.    ' must be no Win32 support, so just return the short filename
  264.    ChangeShortFilenameToLong = True
  265.    gLongFilename = Filename
  266.    gShortFilename = Filename
  267.    Exit Function
  268. End Function
  269. ' copyright 1996, Internet Software Engineering
  270. Function ChopPath (fn As String)
  271. ' returns the filename part of a combined pathfilename
  272. ' e.g. if passed c:\temp\text.txt returns text.txt
  273. Dim x As Integer
  274. Dim y As String
  275.     For x = Len(fn) To 1 Step -1
  276.    If Mid$(fn, x, 1) = "\" Then
  277.        ChopPath = Mid$(fn, x + 1, Len(fn) - x)
  278.        Exit Function
  279.    End If
  280.     Next
  281. End Function
  282. Sub comboFileTypes_Click ()
  283.    Dim tmpstr As String
  284.    tmpstr = comboFileTypes.Text
  285.    txtFilename.Text = GetFileMask(tmpstr)
  286.    FillFileListBox (lblFolders.Caption)
  287. End Sub
  288. ' copyright 1996, Internet Software Engineering
  289. Sub Dir1_Change ()
  290.    Dim a As Long
  291.    Dim sFF As WIN32_FIND_DATA
  292.    Dim p As Integer, q As Integer
  293.    Dim szDirectoryName As String
  294.    Dim label1caption As String
  295.    On Error GoTo Dir1_Change_Error
  296.    If Not gIn16BitSystem Then
  297.       ' get the long filename of the directory
  298.       a = FindFirstFileA(Dir1.Path, sFF, lpFindFirstFileA, 3&, 2&)
  299.       ' clean it up
  300.       p = InStr(sFF.cFileName, Chr$(0))
  301.       szDirectoryName = LCase$(Left$(sFF.cFileName, p - 1))
  302.    Else
  303.       szDirectoryName = Dir1.Path
  304.    End If
  305.    ' going to use lblFolders.Caption several times,
  306.    ' so assign it to a variable for optimization
  307.    label1caption = lblFolders.Caption
  308.    ' check if it's already part of the path in the label
  309.    p = InStr(LCase$(label1caption), szDirectoryName)
  310.    ' if it is, then chop off anything following this name,
  311.    ' but retaining the trailing \
  312.    If Not gIn16BitSystem Then
  313.       If p > 0 Then
  314.      q = InStr(p, label1caption, "\")
  315.      lblFolders.Caption = Left$(label1caption, q)
  316.       ' otherwise add this new name to the end,
  317.       ' always appending a \
  318.       Else
  319.      If Right$(label1caption, 1) <> "\" Then label1caption = label1caption & "\"
  320.      lblFolders.Caption = label1caption & szDirectoryName & "\"
  321.       End If
  322.    ' don't need to chop if in 16 bit system as szDirectoryName will be correct
  323.    Else
  324.       lblFolders.Caption = szDirectoryName
  325.    End If
  326.    ' update the list box with the file names from this new directory
  327.    FillFileListBox (lblFolders.Caption)
  328.    Exit Sub
  329. Dir1_Change_Error:
  330.    Exit Sub
  331. End Sub
  332. Sub Drive1_Change ()
  333.    Dir1.Path = Drive1.Drive
  334.    lblFolders.Caption = Dir1.Path
  335. End Sub
  336. ' copyright 1996, Internet Software Engineering
  337. Sub FillFileListBox (directory As String)
  338.    Dim sFF As WIN32_FIND_DATA
  339.    Dim a As Long
  340.    Dim szShortFilename As String * 256
  341.    Dim tmpstr As String
  342.    Dim p As Integer
  343.    Dim q As Integer
  344.    Dim szFilename As String
  345.    Dim szFileMask As String
  346.    On Error GoTo FillFileListBox_Error
  347.    List1.Clear
  348.    If Not gIn16BitSystem Then
  349.       ' get the short name for the directory currently selected and clean it up
  350.       a = GetShortPathNameA(directory, szShortFilename, 256&, lpGetShortPathNameA, 6&, 3&)
  351.       p = InStr(szShortFilename, Chr$(0))
  352.       szFilename = LCase$(Left$(szShortFilename, p - 1))
  353.    Else
  354.       ' no Win32 support, so go with unaltered directory name
  355.       szFilename = directory
  356.    End If
  357.    If Right$(szFilename, 1) <> "\" Then szFilename = szFilename & "\"
  358.    ' set the mask for the selected file type
  359.    tmpstr = comboFileTypes.Text
  360.    szFileMask = GetFileMask(tmpstr)
  361.    ' fill the list box with the proper file names
  362.    tmpstr = Dir$(szFilename & szFileMask)
  363.    Do
  364.       If tmpstr = "" Then Exit Do
  365.       If Not gIn16BitSystem Then
  366.      'Use the Win32 call to convert any short filenames to long filenames
  367.      a = FindFirstFileA(szFilename & tmpstr, sFF, lpFindFirstFileA, 3&, 2&)
  368.      List1.AddItem sFF.cFileName
  369.       Else
  370.      ' no Win32 support, so go with unaltered filename
  371.      List1.AddItem tmpstr
  372.       End If
  373.       tmpstr = Dir$
  374.    Loop
  375.    List1.Refresh
  376.    Exit Sub
  377. FillFileListBox_Error:
  378.    Exit Sub
  379. End Sub
  380. ' copyright 1996, Internet Software Engineering
  381. Sub FillFileTypesBox (Filter As String, FilterIndex As Integer)
  382.    On Error Resume Next
  383.    Dim p As Integer
  384.    Dim q As Integer
  385.    Dim x As Integer
  386.    p = 1
  387.    q = InStr(Filter, "|")
  388.    If q = 0 Then  ' invalid filter specified, so use default
  389.       comboFileTypes.AddItem "Text (*.txt)"
  390.       comboFileTypes.AddItem "All Files (*.*)"
  391.       comboFileTypes.Text = "Text (*.txt)"
  392.       txtFilename.Text = "*.txt"
  393.       Exit Sub
  394.    End If
  395.    ' append a | for easier processing
  396.    If Right$(Filter, 1) <> "|" Then Filter = Filter & "|"
  397.    x = 1
  398.    Do While q
  399.       comboFileTypes.AddItem Mid$(Filter, p, q - p)
  400.       If x = FilterIndex Then comboFileTypes.Text = Mid$(Filter, p, q - p)
  401.       p = q + 1
  402.       q = InStr(p, Filter, "|")
  403.       If x = FilterIndex Then txtFilename.Text = Mid$(Filter, p, q - p)
  404.       p = q + 1
  405.       q = InStr(p, Filter, "|")
  406.       x = x + 1
  407.    Loop
  408. End Sub
  409. Sub Form_Activate ()
  410.    ' initialize the directory list and file list box
  411. '   Dir1.Path = Drive1.Drive
  412.    lblFolders.Caption = Dir1.Path
  413.    If Right$(lblFolders.Caption, 1) <> "\" Then lblFolders.Caption = lblFolders.Caption & "\"
  414.    FillFileListBox (Dir1.Path)
  415. End Sub
  416. ' copyright 1996, Internet Software Engineering
  417. Sub Form_Load ()
  418.    On Error GoTo DontGotWin32
  419.    ' setup appearance from LongFile structure
  420.    ' use defaults if not otherwise set
  421.    If LF.Color > 0 Then
  422.       BackColor = LF.Color
  423.       lblFolders.BackColor = LF.Color
  424.       Label2.BackColor = LF.Color
  425.       Label3.BackColor = LF.Color
  426.       Label4.BackColor = LF.Color
  427.       Label5.BackColor = LF.Color
  428.    Else
  429.       BackColor = &HC0C0C0
  430.    End If
  431.    If LF.DialogTitle <> "Select File" And LF.DialogTitle <> "" Then
  432.       Caption = LF.DialogTitle
  433.    Else
  434.       Caption = "Select File"
  435.    End If
  436.    If LF.Filter <> "Text (*.txt)|*.txt|All Files (*.*)|*.*" And LF.Filter <> "" Then
  437.       Call FillFileTypesBox(LF.Filter, LF.FilterIndex)
  438.    Else
  439.       Call FillFileTypesBox("Text (*.txt)|*.txt|All Files (*.*)|*.*", 1)
  440.    End If
  441.    If LF.Filename <> "" Then
  442.       Drive1.Drive = GetDrive(LF.Filename)
  443.       txtFilename.Text = ChopPath(LF.Filename)
  444.       lblFolders.Caption = GetPath(LF.Filename)
  445.    End If
  446.    ' load Kernel32
  447.    hInstKernel = LoadLibraryEx32W("Kernel32.dll", 0&, 0&)
  448.    ' get the address of the functions to deal with long filenames
  449.    lpGetShortPathNameA = GetProcAddress32W(hInstKernel, "GetShortPathNameA")
  450.    lpFindFirstFileA = GetProcAddress32W(hInstKernel, "FindFirstFileA")
  451.    gIn16BitSystem = False
  452.    Exit Sub
  453. DontGotWin32:
  454.    ' if here, then long filenames aren't supported,
  455.    ' so set the gIn16BitSystem flag to true and set up
  456.    ' for short filenames
  457.    gIn16BitSystem = True
  458.    Exit Sub
  459. End Sub
  460. Sub Form_Unload (Cancel As Integer)
  461.    On Error Resume Next
  462.    ' reset the LongFile structure
  463.    If LF.Action <> -1 Then LF.Action = 0
  464.    LF.Filename = ""
  465.    If gIn16BitSystem = True Then Exit Sub
  466.    Dim a As Long
  467.    ' release the Kernel if necessary
  468.    a = FreeLibrary32W(hInstKernel)
  469. End Sub
  470. ' copyright 1996, Internet Software Engineering
  471. Function GetDrive (fn As String)
  472. ' returns the drive part of a path filename
  473. ' the trailing \ is not returned
  474. ' e.g. if passed c:\temp\text.txt returns c:
  475. Dim x As Integer
  476.     x = InStr(fn, ":")
  477.     If x > 0 Then
  478.       GetDrive = Left$(fn, x)
  479.     Else
  480.       GetDrive = ""
  481.     End If
  482. End Function
  483. ' copyright 1996, Internet Software Engineering
  484. Function GetFileMask (mask As String) As String
  485.    Dim tmpstr As String
  486.    Dim p As Integer
  487.    Dim q As Integer
  488.    Dim x As Integer
  489.    tmpstr = mask
  490.    p = InStr(tmpstr, "(")
  491.    If p = 0 Then
  492.       MsgBox "Invalid file type in List of File Types", 48, App.Title
  493.       Exit Function
  494.    End If
  495.    p = p + 1
  496.    q = InStr(p, tmpstr, ")")
  497.    If q = 0 Then
  498.       MsgBox "Invalid file type in List of File Types", 48, App.Title
  499.       Exit Function
  500.    End If
  501.    GetFileMask = Mid$(tmpstr, p, q - p)
  502. End Function
  503. ' copyright 1996, Internet Software Engineering
  504. Function GetPath (fn As String)
  505. ' returns the path from a combined pathfilename
  506. ' the trailing \ is not returned
  507. ' e.g. if passed c:\temp\text.txt returns c:\temp
  508. Dim x As Integer
  509. Dim y As String
  510.    For x = Len(fn) To 1 Step -1
  511.       If Mid$(fn, x, 1) = "\" Then
  512.       GetPath = Mid$(fn, 1, x - 1)
  513.       Exit Function
  514.       End If
  515.    Next
  516. End Function
  517. Sub List1_Click ()
  518.    txtFilename.Text = List1.List(List1.ListIndex)
  519. End Sub
  520. Sub List1_DblClick ()
  521.    txtFilename.Text = List1.List(List1.ListIndex)
  522.    btnOpen_Click
  523. End Sub
  524.